var componentinfo= ["./component/base/mmState","./src/js/formview.js","./src/js/tableview.js","./src/js/freeorderview.js","./src/js/toolsview.js","./src/js/doublelistview.js","./src/js/pagerview.js","./src/js/sliderview.js","./src/js/barsview.js","./src/js/mapview.js","./src/js/visa_information_inquiries.js","./src/js/visa_status.js","./src/js/notification_query.js","./src/js/notification_detail.js","./src/js/visa_elements.js","./src/js/visa_request.js","./src/js/visa_receive.js","./component/tab/avalon.tab","./component/dropdownmenu/avalon.dropdownmenu"];

var jsoninfo = [{title:"首页",icon:"home_icon",open:true,innermenuinfo:[
    {title:"欢迎页面",state:"home",src:"#!/",icon:"home_icon",url:"/"},
    {title:"form展现",state:"formview",src:"#!/formview",icon:"form_icon",templateUrl:"formview.html",url:"/formview"},
    {title:"table展现",state:"tableview",src:"#!/tableview",icon:"table_icon",templateUrl:"tableview.html",url:"/tableview"},
    {title:"工具展现",state:"toolsview",src:"#!/toolsview",icon:"tool_icon",templateUrl:"toolsview.html",url:"/toolsview"},
    {title:"订单样例",state:"freeorderview",src:"#!/freeorderview",icon:"map_icon",templateUrl:"freeorderview.html",url:"/freeorderview"}
]},
    {title:"控件详细",icon:"table_icon",open:false,innermenuinfo:[
        {title:"平衡木api",state:"doublelistview",src:"#!/doublelistview",icon:"form_icon",templateUrl: 'doublelistview.html',url: "/doublelistview"},
        {title:"分页api",state:"pagerview",src:"#!/pagerview",icon:"form_icon",templateUrl: 'pagerview.html',url: "/pagerview"},
        {title:"滑块api",state:"sliderview",src:"#!/sliderview",icon:"form_icon",templateUrl: 'sliderview.html',url: "/sliderview"}
    ]},{title:"结合外部API",icon:"form_icon",open:false,innermenuinfo:[
        {title:"bars展现",state:"barsview",src:"#!/barsview",icon:"bars_icon",templateUrl:"barsview.html",url:"/barsview"},
        {title:"map展现",state:"mapview",src:"#!/mapview",icon:"map_icon",templateUrl:"mapview.html",url:"/mapview"}
    ]},{title:"遨游ERP演示",icon:"form_icon",open:true,innermenuinfo:[
        {title:"签证状态查询",state:"visa_status",src:"#!/visa_status",icon:"bars_icon",templateUrl:"visa_status.html",url:"/visa_status"},
        {title:"通知单查询",state:"notification_query",src:"#!/notification_query",icon:"map_icon",templateUrl:"notification_query.html",url:"/notification_query"},{title:"各国签证须知查询",state:"visa_information_inquiries",src:"#!/visa_information_inquiries",icon:"map_icon",templateUrl:"visa_information_inquiries.html",url:"/visa_information_inquiries"}
    ]}
];

var menuinfo = '<ul class="aoyou-tab-menu" ms-visible="menushow" ms-css-left="menuleft" ms-css-top="menutop">'+
    '    <li class="aoyou-tab-menu-bottomborder" ms-click="menurefresh">刷新</li>'+
    '    <li ms-click="menuclose($event)">关闭</li>'+
    '    <li ms-click="menucloseall($event)">全部关闭</li>'+
    '    <li ms-click="menucloseother($event)" class="aoyou-tab-menu-bottomborder">除此之外全部关闭</li>'+
    '    <li ms-click="menucloseright($event)">当前页右侧全部关闭</li>'+
    '    <li ms-click="menucloseleft($event)">当前页左侧全部关闭</li>'+
    '</ul>';


require(componentinfo, function () {
    var vm = avalon.define({
        $id: "test",
        currPath:"",
        menushow:true,
        titleshow:true,
        $dropdownmenuconfig1:{
            mainiconclass:'user',
            outerclass:'top_content_user',
            data:[{content:"消息查看",href:"#",icontype:"mail"},{content:"修改密码",href:"#",icontype:"key"},{content:"退出",href:"#",icontype:"logout"}]
        },
        $dropdownmenuconfig2:{
            messagenum:90,
            mainiconclass:'mail',
            outerclass:'top_content_mail',
            data:[{content:"系统消息",href:"#",messagenum:'80'},{content:"私信",href:"#",messagenum:'10'}]
        },
        frametab:{
            active:0,//必须提前设定默认值否则更改无效
            target:"_self",
            removable: true,
            event:"mousedown",
            nextEnable:1,
            prevEnable:1,
            menushow:false,
            menuleft:0,
            menutop:0,
            menu:menuinfo,
            //刷新按钮
            onmenurefresh:function ($event,vvmm) {
                frames.location.reload()
            },
            //关闭按钮 关闭页面部分
            onmenuclose:function ($event,vvmm) {
                vvmm.menushow = false;
            },
            onmenucloseall:function ($event,vvmm) {
                vvmm.menushow = false;
                $('#frameid').attr('src','');
            },
            onRightMouse:function (event,index,vvmm) {
                //判断鼠标点击事件,2为鼠标右键,弹出右键相应菜单
                vvmm.menushow = true;
                vvmm.menuleft = event.pageX-215;
                vvmm.menutop = event.pageY-50;
                $('#frameid').attr('src',event.target.href);
            },
            onSlider:function(obj,idx){
                avalon.router.go(obj.name||"home");
            },
            afterRemove:function(obj,idx){//删除某个tab之后跳转回前一个页面
                avalon.router.go(obj.name||"home");
                avalon.router.lastHash = obj.name||'home';//保证最后的历史记录
            },
            tabs:[{title: "欢迎光临",href: "#!/",removable: false}]
        },
        menuinfo1:jsoninfo,
        openinner:function(i){
            if(vm.titleshow){
                vm.menuinfo1[i].open = !vm.menuinfo1[i].open;
            }
        },
        controlmenu:function(){
            if(vm.menushow){
                vm.titleshow = false;
                avalon.each(vm.menuinfo1,function(o,e){
                    e.open = false;
                });
                $('.menu').animate({
                    width: "45px",
                    overflowX:"hidden",
                    overflowY:"hidden"
                }, 500 );
                $('.main').animate({
                    left: "45px"
                }, 500);
            }else{
                $('.menu').animate({
                    width: "218px"
                }, 500 );
                $('.main').animate({
                    left: "218px"
                }, 500,null,function(){
                    vm.titleshow = true;
                });
            }
            vm.menushow = !vm.menushow;
        }
    });
    var tabOpt = {
        target:"_self",
        removable: true,
        event:"mousedown",
        iconshow:true,
        iconclass:"text"
    }

    var _obj = [];
    var _temp = jsoninfo.$model;
    var _visa = {
        icon:"map_icon",
        src: "#!/visa_elements",
        state: "visa_elements",
        templateUrl: "visa_elements.html",
        title: "签证要素",
        url: "/visa_elements"
    }
    _obj.push(_visa);

    var _notification = {
        icon:"map_icon",
        src: "#!/notification_detail",
        state: "notification_detail",
        templateUrl: "notification_detail.html",
        title: "通知单详情",
        url: "/notification_detail"
    }
    _obj.push(_notification);

    var _visarequest = {
        icon:"map_icon",
        src: "#!/visa_request",
        state: "visa_request",
        templateUrl: "visa_request.html",
        title: "签证要求",
        url: "/visa_request"
    }
    _obj.push(_visarequest);

    var _visareceive = {
        icon:"map_icon",
        src: "#!/visa_receive",
        state: "visa_receive",
        templateUrl: "visa_receive.html",
        title: "收资料",
        url: "/visa_receive"
    }
    _obj.push(_visareceive);


    for(var i=0,l=_temp.length;i<l;i++){
        if(_temp[i].innermenuinfo != undefined && _temp[i].innermenuinfo.length>0){
            for(var z in _temp[i].innermenuinfo){
                _obj.push((_temp[i].innermenuinfo)[z]);
            }
        }
    }
    avalon.each(_obj,function(o,e){
        var _title = e.title;
        var _state = e.state;
        var _src = e.src;
        var _url = e.url;
        var _turl = e.templateUrl || "/";
        if(_url != "/"){
            avalon.state(_state, {
                url: _url,
                views: {
                    "main": {
                        templateUrl: _turl
                    }
/*                    ,"hint@": {
                        template: "当前状态是"+_title
                    }*/
                },
                onEnter:function(){
                    var frameTab = avalon.vmodels['frameTab'];
                    var index =-1;
                    if(frameTab){
                        var ts = frameTab.tabs;
                        if(ts){
                            for(var i=0,l=ts.$model.length;i<l;i++){
                                if(ts.$model[i].href == _src){
                                    index = i;
                                    break;
                                }
                            }
                        }
                        tabOpt.name = _state;
                        tabOpt.title = _title;
                        tabOpt.href = _src;
                        if(index == -1){
                            frameTab.add(tabOpt);
                            frameTab.active = ts.$model.length-1;
                        }else{
                            frameTab.active = index;
                        }
                    }else{
                        if(this.stateName != "" && this.stateName != "home"){
                            tabOpt.name = _state;
                            tabOpt.title = _title;
                            tabOpt.href = _src;
                            vm.frametab.tabs.push(tabOpt);
                            vm.frametab.active = vm.frametab.tabs.length-1;
                        }else{
                            avalon.router.go("home");//页面刷新自动返回欢迎页
                        }
                    }
                }
            });
        }else{
            avalon.state("home", {
                url: "/",
                views: {
                    "main": {
                        template:'Welcome!'
                        /*,templateProvider: new Promise(function(fn) {
                         fn('Welcome!')
                         })*/
                    }
                },
                onEnter:function(){//如果是欢迎页则直接跳回0
                    var frameTab = avalon.vmodels['frameTab'];
                    if(frameTab){
                        frameTab.active = 0;
                    }
                }
            });
        }
    });

    avalon.history.start({
        basepath: "/home"
    });
    var vms = [vm];//加载各个页面的VM
    for(var i=0,l = arguments.length;i<l;i++){
        if(arguments[i] && arguments[i].$id){
            vms.push(arguments[i]);
        }
    }
    avalon.scan(document.body,vms);
});